package com.wdd.myplatform.controller.wechat;

import com.alibaba.fastjson2.JSONObject;
import com.wdd.myplatform.common.Result;
import com.wdd.myplatform.service.IWeChatService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author Administrator
 */
@RequestMapping("/weChatController")
@RestController
@Slf4j
@Api(tags = "微信（公众号）接口文档")
public class WeChatController {

    @Autowired
    private IWeChatService iWeChatService;

    @GetMapping("/web/getWeChatUserInfoByCode")
    public Result<?> getWeChatUserInfoByCode(@RequestParam String code){
        return iWeChatService.getWeChatUserInfoByCode(code);
    }

    @GetMapping("/web/getWeChatUserInfoByOpenId")
    public Result<?> getWeChatUserInfoByOpenId(@RequestParam String openId){
        return iWeChatService.getWeChatUserInfoByOpenId(openId);
    }

    /**
     * 获取普通accessToken
     * @return
     */
    @GetMapping("/getNormalAccessToken")
    public Result<?> getNormalAccessToken(){
        return iWeChatService.getNormalAccessToken();
    }

    /**
     * 公众号创建菜单
     * @param jsonObject
     * @return
     */
    @PostMapping("/createMenu")
    public Result<?> createMenu(@RequestBody JSONObject jsonObject){
        return iWeChatService.createMenu(jsonObject);
    }

    /**
     * 微信生成预支付交易单
     * @param jsonObject
     * @return
     */
    @PostMapping("/pay/createOrder")
    public Result<?> CreateOrder(@RequestBody JSONObject jsonObject) throws Exception {
        return iWeChatService.createOrder(jsonObject);
    }

    /**
     * 订单支付结果通知
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/pay/notify")
    public Map<String,String> notify(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return iWeChatService.notify(request,response);
    }

    /**
     * 根据商户订单号查询订单
     * @param outTradeNo
     * @return
     */
    @GetMapping("/pay/queryOrderByOutTradeNo")
    public Result<?> queryOrderByOutTradeNo(@RequestParam(value = "outTradeNo",required = true) String outTradeNo) throws Exception {
        return iWeChatService.queryOrderByOutTradeNo(outTradeNo);
    }

    /**
     * 根据微信支付订单号查询订单
     * @param transactionId
     * @return
     */
    @GetMapping("/pay/queryOrderByTransactionId")
    public Result<?> queryOrderByTransactionId(@RequestParam(value = "transactionId",required = true) String transactionId) throws Exception {
        return iWeChatService.queryOrderByTransactionId(transactionId);
    }

    /**
     * 关闭订单
     * @param outTradeNo
     * @return
     */
    @PostMapping("/pay/closeOrder")
    public Result<?> closeOrder(String outTradeNo) throws Exception {
        return iWeChatService.closeOrder(outTradeNo);
    }

    /**
     * 公众号向微信用户推送模板消息
     * 需要配置模板（选择行业）
     * @param jsonObject
     * @return
     */
    @PostMapping("/template/sendMsg")
    public Result<?> sendTemplateMsgToWxUser(@RequestBody JSONObject jsonObject) throws Exception {
        return iWeChatService.sendTemplateMsgToWxUser(jsonObject);
    }

}
